package com.huabo.system.service.impl;


import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblSystemOrgRightMySql;
import com.huabo.system.mysql.entity.TblSystemRightMySql;
import com.huabo.system.mysql.mapper.TblSystemOrgRightMySqlMapper;
import com.huabo.system.mysql.mapper.TblSystemRightMySqlMapper;
import com.huabo.system.oracle.entity.TblSystemOrgRight;
import com.huabo.system.oracle.entity.TblSystemRight;
import com.huabo.system.oracle.mapper.*;
import com.huabo.system.service.TblSystemRightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("TblSystemRightService")
public class TblSystemRightServiceImpl implements TblSystemRightService {
    private static final Logger logger = LoggerFactory.getLogger(TblSystemRightServiceImpl.class);

    @Resource
    private TblManageRightDAO tblManageRightDAO;

    @Resource
    private TblManageUserRightMapper tblManageUserRightMapper;

    @Resource
    private TblSystemRightMapper tblSystemRightMapper;

    @Resource
    private TblSystemOrgRightMapper tblSystemOrgRightMapper;

    @Resource
    private TblSystemRightMySqlMapper tblSystemRightMySqlMapper;

    @Resource
    private TblSystemOrgRightMySqlMapper tblSystemOrgRightMySqlMapper;

    @Resource
    private TblStaffMapper tblStaffMapper;

    @Override
    public JsonBean saveManageRight(TblSystemRight right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        if (right.getId() != null) {
            this.tblSystemRightMapper.updateTblSystemRight(right);
        } else {
            this.tblSystemRightMapper.insertTblManageRight(right);
        }
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getId());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean saveMySqlManageRight(TblSystemRightMySql right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        if (right.getId() != null) {
            this.tblSystemRightMySqlMapper.updateTblSystemRight(right);
        } else {
            this.tblSystemRightMySqlMapper.insertTblManageRight(right);
        }
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getId());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean findRightEntityById(BigDecimal id) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblSystemRight right = this.tblSystemRightMapper.findById(id);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("right", right);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblSystemRightMySql right = this.tblSystemRightMySqlMapper.findById(id);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("right", right);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean modifyManageRight(TblSystemRight right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        this.tblSystemRightMapper.updateTblSystemRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getId());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean modifyMySqlManageRight(TblSystemRightMySql right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        this.tblSystemRightMySqlMapper.updateTblSystemRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getId());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    @Transactional(value = "oracleDataSourceTransactionManager", rollbackFor = Exception.class, timeout = 36000)
    public JsonBean removeManageRight(BigDecimal rightId) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            Integer count = this.tblSystemRightMapper.selectChildrenCount(rightId);
            if (count > 0) {
                return ResponseFormat.retParam(1, 20007, null);
            }
            this.tblSystemRightMapper.deleteOrgRIght(rightId);
            this.tblSystemRightMapper.deleteRoleRIght(rightId);
            this.tblSystemRightMapper.deleteSystemRIght(rightId);
            return ResponseFormat.retParam(1, 200, null);
        } else {
            Integer count = this.tblSystemRightMySqlMapper.selectChildrenCount(rightId);
            if (count > 0) {
                return ResponseFormat.retParam(1, 20007, null);
            }
            this.tblSystemRightMySqlMapper.deleteOrgRIght(rightId);
            this.tblSystemRightMySqlMapper.deleteRoleRIght(rightId);
            this.tblSystemRightMySqlMapper.deleteSystemRIght(rightId);
            return ResponseFormat.retParam(1, 200, null);
        }
    }

    @Override
    public JsonBean findRightLIstByUser(String token, Integer rightId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = 0;
            }

            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            this.setChidrenRightList(rightList, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = 0;
            }

            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            this.setMySqlChidrenRightList(rightList, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void setChidrenRightList(List<TblSystemRight> rightList, BigDecimal staffid, BigDecimal orgid, String moduleType) throws Exception {
        List<TblSystemRight> childrenList = null;
        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectChildrenRightListByUser(right.getId().intValue(), staffid, orgid, moduleType);
            this.setChidrenRightList(childrenList, staffid, orgid, moduleType);
            right.setChildren(childrenList);
        }
    }

    private void setMySqlChidrenRightList(List<TblSystemRightMySql> rightList, BigDecimal staffid, BigDecimal orgid, String moduleType) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectChildrenRightListByUser(right.getId().intValue(), staffid, orgid, moduleType);
            this.setMySqlChidrenRightList(childrenList, staffid, orgid, moduleType);
            right.setChildren(childrenList);
        }
    }

    @Override
    public JsonBean findChildrenRightListByUser(String token, Integer rightId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = 0;
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = 0;
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean findAllRightListByUser(String token, Integer rightId) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectAllRightListByCompanye(rightId, loginStaff.getLinkOrg().getOrgid(), null);
            this.setChidrenRightListByCompany(rightList, loginStaff.getLinkOrg().getOrgid());
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectAllRightListByCompanye(rightId, loginStaff.getLinkOrg().getOrgid(), null);
            this.setMySqlChidrenRightListByCompany(rightList, loginStaff.getLinkOrg().getOrgid());
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void setChidrenRightListByCompany(List<TblSystemRight> rightList, BigDecimal orgid) {
        List<TblSystemRight> childrenList = null;
        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectAllRightListByCompanye(right.getId().intValue(), orgid, null);
            this.setChidrenRightListByCompany(childrenList, orgid);
            right.setChildren(childrenList);
        }
    }

    private void setMySqlChidrenRightListByCompany(List<TblSystemRightMySql> rightList, BigDecimal orgid) {
        List<TblSystemRightMySql> childrenList = null;
        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectAllRightListByCompanye(right.getId().intValue(), orgid, null);
            this.setMySqlChidrenRightListByCompany(childrenList, orgid);
            right.setChildren(childrenList);
        }
    }

    private void setChidrenRightListByCompanyAndRole(List<TblSystemRight> rightList, BigDecimal orgid, Integer roleId) throws Exception {
        List<TblSystemRight> childrenList = null;

        Integer count = 0;

        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectAllRightListByCompanye(right.getId().intValue(), orgid, null);
            count = this.tblSystemRightMapper.judgeGrantRightByRoleId(right.getId(), roleId);
            if (count > 0) {
                right.setChecked(true);
            } else {
                right.setChecked(false);
            }
            this.setChidrenRightListByCompanyAndRole(childrenList, orgid, roleId);
            right.setChildren(childrenList);
        }

    }

    private void setMySqlChidrenRightListByCompanyAndRole(List<TblSystemRightMySql> rightList, BigDecimal orgid, Integer roleId) throws Exception {
        List<TblSystemRightMySql> childrenList = null;

        Integer count = 0;

        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectAllRightListByCompanye(right.getId().intValue(), orgid, null);
            count = this.tblSystemRightMySqlMapper.judgeGrantRightByRoleId(right.getId(), roleId);
            if (count > 0) {
                right.setChecked(true);
            } else {
                right.setChecked(false);
            }
            this.setMySqlChidrenRightListByCompanyAndRole(childrenList, orgid, roleId);
            right.setChildren(childrenList);
        }

    }

    @Override
    public JsonBean findAllRightListByCompany(String token, Integer rightId, Integer orgId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectAllRightListByFatherCompany(rightId, loginStaff.getLinkOrg().getOrgid().intValue(), moduleType);
            this.setChidrenRightListByFatherCompany(rightList, loginStaff.getLinkOrg().getOrgid(), orgId, moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectAllRightListByFatherCompany(rightId, loginStaff.getLinkOrg().getOrgid().intValue(), moduleType);
            this.setMySqlChidrenRightListByFatherCompany(rightList, loginStaff.getLinkOrg().getOrgid(), orgId, moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void setChidrenRightListByFatherCompany(List<TblSystemRight> rightList, BigDecimal orgid, Integer checkOrgid, String moduleType) throws Exception {
        List<TblSystemRight> childrenList = null;
        Integer count = 0;
        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectAllRightListByFatherCompany(right.getId().intValue(), orgid.intValue(), moduleType);
            count = this.tblSystemRightMapper.judgeGrantRightByOrgid(right.getId(), checkOrgid);
            if (count > 0) {
                right.setChecked(true);
            } else {
                right.setChecked(false);
            }
            this.setChidrenRightListByCompany(childrenList, orgid);
            right.setChildren(childrenList);
        }
    }

    private void setMySqlChidrenRightListByFatherCompany(List<TblSystemRightMySql> rightList, BigDecimal orgid, Integer checkOrgid, String moduleType) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        Integer count = 0;
        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectAllRightListByFatherCompany(right.getId().intValue(), orgid.intValue(), moduleType);
            count = this.tblSystemRightMySqlMapper.judgeGrantRightByOrgid(right.getId(), checkOrgid);
            if (count > 0) {
                right.setChecked(true);
            } else {
                right.setChecked(false);
            }
            this.setMySqlChidrenRightListByCompany(childrenList, orgid);
            right.setChildren(childrenList);
        }
    }

    @Override
    public JsonBean grantUserRight(String rightIds, String token, Integer staffId) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            //移除之前的权限
            this.tblSystemRightMapper.removeUserRigthAll(staffId);
            String[] rightId = rightIds.split(",");
            for (String id : rightId) {
                this.tblSystemRightMapper.saveUserRight(id, staffId);
            }
            return ResponseFormat.retParam(1, 200, null);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            //移除之前的权限
            this.tblSystemRightMySqlMapper.removeUserRigthAll(staffId);
            String[] rightId = rightIds.split(",");
            for (String id : rightId) {
                this.tblSystemRightMySqlMapper.saveUserRight(id, staffId);
            }
            return ResponseFormat.retParam(1, 200, null);
        }
    }

    @Override
    public JsonBean grantCompanyRight(String rightIds, String token, String orgId,String moduleType) throws Exception {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            //移除之前的权限
            this.tblSystemRightMapper.removeCompanyRigthAll(orgId,moduleType);
            String[] rightId = rightIds.split(",");
            for (String id : rightId) {
                this.tblSystemRightMapper.saveCompanyIdRight(id, orgId);
            }
            return ResponseFormat.retParam(1, 200, null);
    }

    @Override
    public JsonBean grantCompanyRightMySql(String rightIds, String token, String orgId,String moduleType) throws Exception {

            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            //移除之前的权限
            this.tblSystemRightMySqlMapper.removeCompanyRigthAll(orgId,moduleType);
            String[] rightId = rightIds.split(",");
            for (String id : rightId) {
                this.tblSystemRightMySqlMapper.saveCompanyIdRight(id, orgId);
            }
            return ResponseFormat.retParam(1, 200, null);

    }

    @Override
    public JsonBean getCompanySettingRightList(String token, Integer rightId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getCurrentOrg().getOrgid(), moduleType);
            this.setChidrenRightList(rightList, loginStaff.getStaffid(), loginStaff.getCurrentOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getCurrentOrg().getOrgid(), moduleType);
            this.setMySqlChidrenRightList(rightList, loginStaff.getStaffid(), loginStaff.getCurrentOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean getCompanySettingRightListInfo(String token, Integer rightId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getCurrentOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectChildrenRightListByUser(rightId, loginStaff.getStaffid(), loginStaff.getCurrentOrg().getOrgid(), moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean findSystemRightSettingById(BigDecimal id) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblSystemRight right = this.tblSystemRightMapper.findSettingRightById(id);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("right", right);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblSystemRightMySql right = this.tblSystemRightMySqlMapper.findSettingRightById(id);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("right", right);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean modifySystemSettingRight(TblSystemRight right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        TblSystemOrgRight orgRight = new TblSystemOrgRight();
        orgRight.setOrgid(loginStaff.getCurrentOrg().getOrgid());
        orgRight.setRightid(right.getId());
        orgRight.setRightname(right.getName());
        orgRight.setRightstatus(right.getVisible());
        this.tblSystemOrgRightMapper.updateSystemSettingRight(orgRight);
        right.setName(null);
        right.setVisible(null);
        this.tblSystemRightMapper.updateTblSystemRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getId());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean modifyMySqlSystemSettingRight(TblSystemRightMySql right, String token) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        TblSystemOrgRightMySql orgRight = new TblSystemOrgRightMySql();
        orgRight.setOrgid(loginStaff.getCurrentOrg().getOrgid());
        orgRight.setRightid(right.getId());
        orgRight.setRightname(right.getName());
        orgRight.setRightstatus(right.getVisible());
        this.tblSystemOrgRightMySqlMapper.updateSystemSettingRight(orgRight);
        right.setName(null);
        right.setVisible(null);
        this.tblSystemRightMySqlMapper.updateTblSystemRight(right);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("rightId", right.getId());
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean findAllRightListcf(String token, TblSystemRight right, Integer judge) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        List<TblSystemRight> rightList = new ArrayList<TblSystemRight>(0);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);


        if (judge != null && 1 == judge) {
            String rightIdStr = "0";
            String rightId = "";
            String parentId = "";
            rightId = this.tblSystemRightMapper.selectRightIdStrByRight(right);
            if (rightId != null && !"".equals(rightId)) {
                rightIdStr += "," + rightId;
                do {
                    parentId = this.tblSystemRightMapper.selectFatherRightIdStrByRight(rightId);
                    if (parentId == null || "".equals(parentId)) {
                        break;
                    }
                    rightIdStr += "," + parentId;
                    rightId = parentId;
                } while (parentId.indexOf(",") != -1);
                rightList = this.tblSystemRightMapper.selectAllRightListByChoice(0, rightIdStr);
                this.setAllChildrenRightListByChoice(rightList, rightIdStr);
                resultMap.put("rightList", rightList);
            } else {
                resultMap.put("rightList", null);
            }
        } else {
            rightList = this.tblSystemRightMapper.selectAllRightList(0);
            this.setAllChildrenRightList(rightList);
            resultMap.put("rightList", rightList);
        }
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    @Override
    public JsonBean findAllMySqlRightListcf(String token, TblSystemRightMySql right, Integer judge) throws Exception {
        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
        List<TblSystemRightMySql> rightList = new ArrayList<>(0);
        Map<String, Object> resultMap = new HashMap<String, Object>(0);


        if (judge != null && 1 == judge) {
            String rightIdStr = "0";
            String rightId = "";
            String parentId = "";
            rightId = this.tblSystemRightMySqlMapper.selectRightIdStrByRight(right);
            if (rightId != null && !"".equals(rightId)) {
                rightIdStr += "," + rightId;
                do {
                    parentId = this.tblSystemRightMySqlMapper.selectFatherRightIdStrByRight(rightId);
                    if (parentId == null || "".equals(parentId)) {
                        break;
                    }
                    rightIdStr += "," + parentId;
                    rightId = parentId;
                } while (parentId.indexOf(",") != -1);
                rightList = this.tblSystemRightMySqlMapper.selectAllRightListByChoice(0, rightIdStr);
                this.setAllMySqlChildrenRightListByChoice(rightList, rightIdStr);
                resultMap.put("rightList", rightList);
            } else {
                resultMap.put("rightList", null);
            }
        } else {
            rightList = this.tblSystemRightMySqlMapper.selectAllRightList(0);
            this.setAllMySqlChildrenRightList(rightList);
            resultMap.put("rightList", rightList);
        }
        return ResponseFormat.retParam(1, 200, resultMap);
    }

    private void setAllChildrenRightListByChoice(List<TblSystemRight> rightList, String rightIdStr) throws Exception {
        List<TblSystemRight> childrenList = null;
        for (TblSystemRight tblSystemRight : rightList) {
            childrenList = this.tblSystemRightMapper.selectAllRightListByChoice(tblSystemRight.getId().intValue(), rightIdStr);
            this.setAllChildrenRightList(childrenList);
            tblSystemRight.setChildren(childrenList);
        }
    }

    private void setAllMySqlChildrenRightListByChoice(List<TblSystemRightMySql> rightList, String rightIdStr) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        for (TblSystemRightMySql tblSystemRight : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectAllRightListByChoice(tblSystemRight.getId().intValue(), rightIdStr);
            this.setAllMySqlChildrenRightList(childrenList);
            tblSystemRight.setChildren(childrenList);
        }
    }

    private void setAllChildrenRightList(List<TblSystemRight> rightList) throws Exception {
        List<TblSystemRight> childrenList = null;
        for (TblSystemRight tblSystemRight : rightList) {
            childrenList = this.tblSystemRightMapper.selectAllRightList(tblSystemRight.getId().intValue());
            this.setAllChildrenRightList(childrenList);
            tblSystemRight.setChildren(childrenList);
        }
    }

    private void setAllMySqlChildrenRightList(List<TblSystemRightMySql> rightList) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        for (TblSystemRightMySql tblSystemRight : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectAllRightList(tblSystemRight.getId().intValue());
            this.setAllMySqlChildrenRightList(childrenList);
            tblSystemRight.setChildren(childrenList);
        }
    }

    @Override
    public JsonBean findRightListByRole(String token, Integer rightId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = 0;
            }

            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            if (loginStaff.getRoleIdStrs() == null || "".equals(loginStaff.getRoleIdStrs())) {
                resultMap.put("rightList", null);
                return ResponseFormat.retParam(1, 200, resultMap);
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectChildrenRightListByRole(rightId, loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            this.setChidrenRightListByRole(rightList, loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            if (rightId == null) {
                rightId = 0;
            }

            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            if (loginStaff.getRoleIdStrs() == null || "".equals(loginStaff.getRoleIdStrs())) {
                resultMap.put("rightList", null);
                return ResponseFormat.retParam(1, 200, resultMap);
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectChildrenRightListByRole(rightId, loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            this.setMySqlChidrenRightListByRole(rightList, loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void setChidrenRightListByRole(List<TblSystemRight> rightList, String roleId, BigDecimal orgid, String moduleType) throws Exception {
        List<TblSystemRight> childrenList = null;
        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectChildrenRightListByRole(right.getId().intValue(), roleId, orgid, moduleType);
            this.setChidrenRightListByRole(childrenList, roleId, orgid, moduleType);
            right.setChildren(childrenList);
        }
    }

    private void setMySqlChidrenRightListByRole(List<TblSystemRightMySql> rightList, String roleId, BigDecimal orgid, String moduleType) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectChildrenRightListByRole(right.getId().intValue(), roleId, orgid, moduleType);
            this.setMySqlChidrenRightListByRole(childrenList, roleId, orgid, moduleType);
            right.setChildren(childrenList);
        }
    }

    @Override
    public JsonBean grantRoleRight(String rightIds, String token, Integer roleId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            //移除之前的权限
            this.tblSystemRightMapper.removeRoleRigthByModuleType(roleId, moduleType);
            String[] rightId = rightIds.split(",");
            for (String id : rightId) {
                this.tblSystemRightMapper.saveRoleRight(id, roleId);
            }
            return ResponseFormat.retParam(1, 200, null);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            //移除之前的权限
            this.tblSystemRightMySqlMapper.removeRoleRigthByModuleType(roleId, moduleType);
            String[] rightId = rightIds.split(",");
            for (String id : rightId) {
                this.tblSystemRightMySqlMapper.saveRoleRight(id, roleId);
            }
            return ResponseFormat.retParam(1, 200, null);
        }
    }

    @Override
    public JsonBean findRoleRigetListByType(String token, Integer type, String moduleType) throws Exception {
    	TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
        if (loginStaff == null) {
            return ResponseFormat.retParam(0, 20006, null);
        }
    	
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            List<TblSystemRight> rightList = new ArrayList<TblSystemRight>(0);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            if (loginStaff.getRoleIdStrs() == null || "".equals(loginStaff.getRoleIdStrs())) {
                resultMap.put("rightList", null);
                return ResponseFormat.retParam(1, 200, resultMap);
            }

            if (type == null) {
                rightList = this.tblSystemRightMapper.selectRoleRigetListByType(loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType, 0);
                this.setChildrenRoleRigetListByType(rightList, loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType, 1);
            } else {
                rightList = this.tblSystemRightMapper.selectRoleRigetListByType(loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType, type);
            }

            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            List<TblSystemRightMySql> rightList = new ArrayList<>(0);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            if (loginStaff.getRoleIdStrs() == null || "".equals(loginStaff.getRoleIdStrs())) {
                resultMap.put("rightList", null);
                return ResponseFormat.retParam(1, 200, resultMap);
            }

            if (type == null) {
                rightList = this.tblSystemRightMySqlMapper.selectRoleRigetListByType(loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType, 0);
                this.setMySqlChildrenRoleRigetListByType(rightList, loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType, 1);
            } else {
                rightList = this.tblSystemRightMySqlMapper.selectRoleRigetListByType(loginStaff.getRoleIdStrs(), loginStaff.getLinkOrg().getOrgid(), moduleType, type);
            }

            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void setChildrenRoleRigetListByType(List<TblSystemRight> rightList, String roleId, BigDecimal orgid, String moduleType, Integer type) throws Exception {
        List<TblSystemRight> childrenList = null;
        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectChildrenRightListByRoleType(right.getId().intValue(), roleId, orgid, moduleType, type);
            right.setChildren(childrenList);
        }
    }

    private void setMySqlChildrenRoleRigetListByType(List<TblSystemRightMySql> rightList, String roleId, BigDecimal orgid, String moduleType, Integer type) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectChildrenRightListByRoleType(right.getId().intValue(), roleId, orgid, moduleType, type);
            right.setChildren(childrenList);
        }
    }

    @Override
    public JsonBean modifySystemRightVisible(String token, Integer rightId, Integer visible) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            this.tblSystemRightMapper.updateSystemRightVisible(rightId, visible);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightId", rightId);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            this.tblSystemRightMySqlMapper.updateSystemRightVisible(rightId, visible);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightId", rightId);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    @Override
    public JsonBean findAllRightListByCompanyToGrant(String token, Integer rightId, Integer roleId, String moduleType) throws Exception {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRight> rightList = this.tblSystemRightMapper.selectAllRightListByCompanye(rightId, loginStaff.getLinkOrg().getOrgid(), moduleType);
            this.sestChildrenRightListByCompanyToGrant(rightList, loginStaff.getLinkOrg().getOrgid(), roleId, moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        } else {
            TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
            if (loginStaff == null) {
                return ResponseFormat.retParam(0, 20006, null);
            }
            List<TblSystemRightMySql> rightList = this.tblSystemRightMySqlMapper.selectAllRightListByCompanye(rightId, loginStaff.getLinkOrg().getOrgid(), moduleType);
            this.sestMySqlChildrenRightListByCompanyToGrant(rightList, loginStaff.getLinkOrg().getOrgid(), roleId, moduleType);
            Map<String, Object> resultMap = new HashMap<String, Object>(0);
            resultMap.put("rightList", rightList);
            return ResponseFormat.retParam(1, 200, resultMap);
        }
    }

    private void sestChildrenRightListByCompanyToGrant(List<TblSystemRight> rightList, BigDecimal orgid, Integer roleId, String moduleType) throws Exception {
        List<TblSystemRight> childrenList = null;
        Integer count = 0;
        for (TblSystemRight right : rightList) {
            childrenList = this.tblSystemRightMapper.selectAllRightListByCompanye(right.getId().intValue(), orgid, moduleType);
            this.setChidrenRightListByCompanyAndRole(childrenList, orgid, roleId);
            count = this.tblSystemRightMapper.judgeGrantRightByRoleId(right.getId(), roleId);
            if (count > 0) {
                right.setChecked(true);
            } else {
                right.setChecked(false);
            }
            this.sestChildrenRightListByCompanyToGrant(childrenList, orgid, roleId, moduleType);
            right.setChildren(childrenList);
        }
    }

    private void sestMySqlChildrenRightListByCompanyToGrant(List<TblSystemRightMySql> rightList, BigDecimal orgid, Integer roleId, String moduleType) throws Exception {
        List<TblSystemRightMySql> childrenList = null;
        Integer count = 0;
        for (TblSystemRightMySql right : rightList) {
            childrenList = this.tblSystemRightMySqlMapper.selectAllRightListByCompanye(right.getId().intValue(), orgid, moduleType);
            this.setMySqlChidrenRightListByCompanyAndRole(childrenList, orgid, roleId);
            count = this.tblSystemRightMapper.judgeGrantRightByRoleId(right.getId(), roleId);
            if (count > 0) {
                right.setChecked(true);
            } else {
                right.setChecked(false);
            }
            this.sestMySqlChildrenRightListByCompanyToGrant(childrenList, orgid, roleId, moduleType);
            right.setChildren(childrenList);
        }
    }

}
